var commentIndex;
$(document).ready(function(){
	initArticle();
});

//剪切url的参数
function getUrl(){
	
}
//初始化文章作者等信息
function initArticle(){
	var url = "/HyperIT/article/getOneArticle";
	var parm;
	if(getCookie("user")==null){
		parm = "article_id="+window.location.href.split("?")[1];
	}else{
		parm = "article_id="+window.location.href.split("?")[1]+"&userdetail_id="+thisUser.id;
	}
	$.get(url,parm,function(data){
		console.log(data);
		var author = data.resultParm.user;
		authorId = author.id;
		var article = data.resultParm.article;
		articleId = article.id;
		var tags = data.resultParm.tag;
		if(data.serviceResult){
			$("#article-title").text(html_decode(article.title));
			$("#author>a").attr("href","../blog/theblog.html?"+author.id);
			$("#author>a>img").attr("src","../../"+author.head);
			$("#author-info #authorname").text(author.name);
			if(data.resultParm.follow){
				$("#author-info>a").eq(0).text("取关").addClass("active").attr("onclick","cancelFollow(authorId)");
			}
			if(data.resultParm.collect){
				$(".action a").eq(0).text("取消收藏").addClass("active").attr("onclick","cancelCollect(articleId,0)");
			}
			$("#article-intro").text(html_decode(article.info));
			$("#reading").text(article.reading);
			$("#reward").text(article.reward);
			$("#collecting").text(article.collecting);
			$("#date").text(article.createtime.substr(0,16));
			$("#article-content").append(html_decode(article.content_html));
			editormd.markdownToHTML("article-content");
			showTags(tags);
			$(".this-commer img").attr("src","../.."+thisUser.head)
			initArticleComment();
		}else{
			alert(data.resultInfo);
		}
	});
}
//显示标签
function showTags(tags){
	for(var i = 0; i<tags.length; i++){
		$(".article-tags").append('<span>'+tags[i].name+'</span>');
	}
}
//发表评论
function say(){
	if(getCookie("user")==null){
		messageTip("哎呀，还没登录呢！");
		return;
	}
	var url = "/HyperIT/blog/comment";
	var parm = "userDetail_id="+thisUser.id+"&target_id="+articleId+"&content="+$("#this-comment").val()+"&type=0";
	$.post(url,parm,function(data){
		if (data.serviceResult) {
			var comment = data.resultParm.comment;
			$(".comments").prepend('<div class="comment-item">'+
						            	'<div class="commer-head">'+
							        		'<a href="../blog/theblog.html?'+comment.userDetail_id+'">'+
							        		'<img src="../..'+thisUser.head+'"/></a>'+
						            	'</div>'+
										'<div class="comment-right">'+
											'<p>'+comment.content+'</p>'+
										'</div>'+
										'<div class="comment-time-action">'+
											'<span>'+comment.createtime.substr(0,16)+'</span>'+
										'</div>'+
									'</div>');
			$("#this-comment").val("");
			messageTip(data.resultInfo);
		} else{
			messageTip(data.resultInfo);
		}
	});
}
//获取文章评论
function getArticleComment(index){
	var url = "/HyperIT/blog/getComment";
	var parm = "size=10&indexPageNum="+index+"&target_id="+articleId+"&type=0";
	$.get(url,parm,function(data){
		if (data.serviceResult) {
			var commentList = data.resultParm.getcomment;
			showArticleComment(commentList);
			commentIndex++;
		} else{
			if (commentIndex==1) {
				return;
			} else{
				messageTip(data.resultInfo);
			}
		}
	});
}
//绘制comment
function showArticleComment(commentList){
	for(var i = 0;i<commentList.length; i++){
		$("a.more").before('<div class="comment-item">'+
						            	'<div class="commer-head">'+
							        		'<a href="../blog/theblog.html?'+commentList[i].userdetail.id+'">'+
							        		'<img src="../..'+commentList[i].userdetail.head+'"/></a>'+
						            	'</div>'+
										'<div class="comment-right">'+
											'<p>'+commentList[i].comment.content+'</p>'+
										'</div>'+
										'<div class="comment-time-action">'+
											'<span>'+commentList[i].comment.createtime.substr(0,16)+'</span>'+
										'</div>'+
									'</div>');
	}
}
//初始化comment
function initArticleComment(){
	$(".comments").children(".comment-item").remove();
	commentIndex = 1;
	getArticleComment(commentIndex);
}
function getMoreArticleComment(){
	getArticleComment(commentIndex);
}
